package com.q.tqb.activity.login;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.q.tqb.R;
import com.q.tqb.activity.HtmlActivity;
import com.q.tqb.activity.MainActivity;
import com.q.tqb.base.AppConst;
import com.q.tqb.base.BaseActivity;
import com.q.tqb.model.UserInfo;
import com.q.tqb.util.LogUtil;
import com.q.tqb.util.SPUtil;
import com.q.tqb.util.StringUtil;
import com.q.tqb.util.http.IResponse;
import com.q.tqb.util.http.OkHttpUtil;


import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;

import butterknife.OnClick;

public class LoginActivity extends BaseActivity {


    @BindView(R.id.et_tel)
    EditText mEtTel;
    @BindView(R.id.et_pass)
    EditText mEtPass;
    @BindView(R.id.tv_login)
    TextView mTvLogin;
    @BindView(R.id.tv_reset_pass)
    TextView mTvResetPass;
    @BindView(R.id.tv_agreement)
    TextView mTvAgreement;

    @Override
    public int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    public void init(Bundle savedInstanceState) {
        setTitle("淘趣包");
        mIvBack.setVisibility(View.GONE);
    }


    @OnClick({R.id.iv_back, R.id.tv_login, R.id.tv_reset_pass, R.id.tv_agreement})
    public void onViewClicked(View view) {
        Intent intent;
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_login:
                telLogin();
                break;
            case R.id.tv_reset_pass:
                intent = new Intent(LoginActivity.this, ResetPassWordActivity.class);
                startActivityForResult(intent, 66);
                break;
            case R.id.tv_agreement:
//                intent = new Intent(LoginActivity.this, HtmlActivity.class);
//                intent.putExtra("title", "用户协议");
//                intent.putExtra("url", "http://m.tqboo.com/home/help/xieyi");
//                startActivity(intent);
                intent = new Intent(LoginActivity.this, RegisterActivity.class);
                startActivityForResult(intent, 66);
                break;
            default:
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 66 && resultCode == RESULT_OK) {
            mEtTel.setText(data.getStringExtra("mobile"));
            mEtPass.setText(data.getStringExtra("pwd"));
            telLogin();
        }
    }

    private void telLogin() {

        String tel = mEtTel.getText().toString();
        String password = mEtPass.getText().toString();

        if (!StringUtil.isMobile(tel)) {
            showToast("请输入正确的手机号");
            return;
        }
        if (TextUtils.isEmpty(password)) {
            showToast("请输入密码");
            return;
        }
        if (password.length() < 6) {
            showToast("密码不能小于6位");
            return;
        }

        Map<String, String> map = new HashMap<>();
        map.put("mobile", tel);
        map.put("password", password);
        mProgressDialog.show();

        OkHttpUtil.getInstance().post(this, AppConst.URL_TEL_LOGIN, map, new IResponse() {
            @Override
            public void onSuccess(JsonElement element) {
                mProgressDialog.dismiss();
                LogUtil.i(element.toString());
                Gson gson = new Gson();
                String result = element.toString().replace("null", "\"\"");
                UserInfo personData = gson.fromJson(result, UserInfo.class);

                showToast("登录成功");

                //昵称      15768566894
                SPUtil.put(LoginActivity.this, "nickname", personData.getNickname());
                //头部图片
                SPUtil.put(LoginActivity.this, "header", personData.getHeader());
                //性别
                SPUtil.put(LoginActivity.this, "sex", personData.getSex());
                //手机
                SPUtil.put(LoginActivity.this, "mobile", personData.getMobile());
                SPUtil.put(LoginActivity.this, "uid", personData.getUid());
                //是否登录
                SPUtil.put(LoginActivity.this, "isLogin", true);

                SPUtil.put(LoginActivity.this, "level", personData.getLevel());
                SPUtil.put(LoginActivity.this, "level_name", personData.getLevel_name());
                SPUtil.put(LoginActivity.this, "is_max_level", personData.getIs_max_level());

//                //支付密码
//                SPUtil.put(LoginActivity.this, "payPassword", personData.getPay_password());
//                //用户uid 唯一标识码
//                SPUtil.put(LoginActivity.this, "uid", personData.getUid());
//                //获得红包金额
//                SPUtil.put(LoginActivity.this, "getMoney", personData.getGet_money());
//                //发红包金额
//                SPUtil.put(LoginActivity.this, "postMoney", personData.getPost_money());
//                //账户余额
//                SPUtil.put(LoginActivity.this, "withdraw", personData.getWithdraw_cash());
//                //是否推客
//                SPUtil.put(LoginActivity.this, "isUserAgent", personData.getIs_users_agent());
//                //是否商户
//                SPUtil.put(LoginActivity.this, "isMerchant", personData.getIs_merchant());
//                //是否登录
//                SPUtil.put(LoginActivity.this, "isLogin", true);
//                //是否对外显示
//                SPUtil.put(LoginActivity.this, "isShow", personData.getIs_show_privacy());
//                //个人签名
//                SPUtil.put(LoginActivity.this, "signature", personData.getSignature());

                setResult(RESULT_OK);
                finish();
            }

            @Override
            public void onFailure(int statusCode, String errorMsg) {
                LogUtil.i(errorMsg + "statusCode" + statusCode);
                showToast(errorMsg);
                mProgressDialog.dismiss();
            }
        });
    }

}
